
VARIABLE NL_PTR
VARIABLE DSR_ERR

ASM: DSRLNK  ( subr -- )
    DSR_ERR @@ CLR,             \ zero DSR_ERR
    $6000 @@ CLR,               \ select bank 1
    NL_PTR @@ $8356 @@ MOV,     \ PAB+9 pointer
    R7 $0420 LI,                \ construct BLWP instruction
    $A00C @@ R8 MOV,            \ to DSRLNK
    *SP+ R9 MOV,                \ pop DSR subroutine (8 or Ah) from stack
    R10 $045B LI,               \ construct B *Rll instruction
    R0 STWP,                    \ get address of workspace
    R0 14 AI,                   \ R0 points to the address of R7
    R0 ** BL,                   \ execute the code starting at r7
    EQ IF,                      \ error? [insures device error passed on]
        R0 R0 MOV,              \ DSRLNK in TF ROM should set R0 to error code
        EQ IF,
            R0 $4000 LI,        \ if 0 then it was a bad device error
        ELSE,
            R0 $8000 LI,        \ else file not found or file type
        ENDIF,
        R0 DSR_ERR @@ MOV,      \ move I/O error to DSR_ERR
    ENDIF,
    $A010 @@ R0 MOV,            \ read pointer to scratchpad restore code
    R0 ** BL,                   \ restore scratchpad
    $6002 @@ CLR,               \ select bank 0
;ASM

CREATE PAB_BUF 10 CHARS ALLOT
: OPT5 ( "filename" -- err )
    FLUSH                       \ purge disk blocks
    $1000 >R                    \ address of PAB in VDP
    PAB_BUF 10 0 FILL           \ zero the pab buffer
    5 PAB_BUF C!                \ load op-code to PAB byte 0
    R@ 24 +  PAB_BUF 2+  !      \ vdp address of loaded data in bytes 2 & 3
    $2000 PAB_BUF 6 +  !        \ max bytes to load in PAB bytes 6 & 7
    DUP PAB_BUF 9 + C!          \ length of file name to PAB byte 9
    R@ 9 + NL_PTR !             \ set NL_PTR with VDP addr of length byte
    R@ PAB_BUF 10 VMBW          \ write pab to VDP
    R> 10 +  -ROT VMBW          \ write filename to VDP PAB
    8 DSRLNK                    \ go load the file
    DSR_ERR @ ?DUP IF 
        CR ." Can't load file. Error #" $.  ABORT
    THEN
;
S" DSK1.DEMO" OPT5
